<?php


namespace app\index\business;


use app\common\model\ConsignorItem;
use app\common\model\ConsignorItemAddMember;
use app\common\model\ConsignorReplenish;
use app\common\model\MemberInfo;
use app\common\model\MemberInfoAddUser;
use think\Exception;

class logisticsBus
{
    public $consignorItemModel = null;
    public $consignorItemAddMemberModel = null;
    public $memberInfoAddUserModel = null;
    public $memberInfoMode = null;
    public $consignorReplenish= null;
    public function __construct()
    {
        $this->consignorItemModel = new ConsignorItem();
        $this->consignorItemAddMemberModel = new ConsignorItemAddMember();
        $this->memberInfoAddUserModel = new MemberInfoAddUser();
        $this->memberInfoMode = new MemberInfo();
        $this->consignorReplenish = new ConsignorReplenish();
    }

    public function getAllListPage($where,$paging){
//        dump($where);exit();
        if(array_key_exists('item_category',$where)){
            $where = [
                'a.member_id' => $where['member_id'],
                'b.item_category' => $where['item_category']
            ];
        }else{
            $where = [
                'a.member_id' => $where['member_id']
            ];
        }
        $res = $this->consignorItemAddMemberModel->getListByWherePage($where,$paging);
        return $res;
    }
    public function getMyMemberInfoAddUserListPageBus($where,$paging){
        $res = $this->memberInfoAddUserModel->getListByPage($where,$paging);
        return $res;
    }
    public function getMyConsignorDetailBus($id,$member_id){
        $isWhere = [
            'member_info_id' => $id,
            'member_id' => $member_id
        ];
        $isData = $this->memberInfoAddUserModel->getListItem($isWhere);
        if(empty($isData)){
            throw new \think\Exception('不是如我的货主企业');
        }
        $visitInfo = $this->memberInfoMode->getMemberInfoById(['member_id'=>$member_id]);
        if(empty($visitInfo)){
            $res = $this->memberInfoMode->getDetailFieldsById($id);
            if(empty($res)){
                throw new Exception('该用户已经注销或者不存在');
            }
            $supplement = null;
        }else{
            if($visitInfo->member_grade==2){
                $res = $this->memberInfoMode->getMemberInfoById(['member_id'=>$id]);
                if(empty($res)){
                    throw new Exception('该用户已经注销或者不存在');
                }
                $supplement=null;
                if($res->member_category==1){
                    $supplement = $this->consignorReplenish->getReplenishInfoByWhere(['member_id'=>$id]);
                }else{
                    throw new Exception('无权查看，稍后返回');
                }
            }else{
                $res = $this->memberInfoMode->getDetailFieldsByWhere(['member_id'=>$id]);
                if(empty($res)){
                    throw new Exception('数据库连接错误');
                }
                $supplement = null;
            }

        }

        $result = [
            "memberInfo"=>$res,
            "supplement"=>$supplement
        ];
        return $result;
    }
}